#!/bin/bash

TidyOutput(){
    awk -F '\r' '{print $NF}' $1 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)*[m|K]//g"
}
    
InitE=$(awk '/Energy/ {printf "% 16.10f\n", $NF; exit}' $1)
echo "#  Energy(kcal/mol)   Gradient(au/bohr) : Initial E = $InitE a.u."
paste <(TidyOutput $1 | awk '/Energy/ {p=$NF; printf "    % 14.6f\n", 0.0} /E / {q=627.51*($(NF-4)-p); if ($NF < -1) {printf "    % 14.6f\n", r} else {printf "    % 14.6f\n", q; r=q}}') <(TidyOutput $1 | sed s/'\/'/' '/g | awk -F '\r' '{print $NF}' | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)*[m|K]//g" | awk '/Gradient/ {printf "%14.6f\n", $6} /Grad[ _]/ {printf "%14.6f\n", $16}')
